package com.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.log4j.Logger;


/**
 * 本地文件复制
 * Created by Administrator on 2017/9/14 0014.
 */
public class LocalFileCopy {
	
	private static Logger logger = Logger.getLogger(LocalFileCopy.class);
	private static int fileCount = 0;
	private static int dirCount = 0;
	  
    public static void main(String[] args) {  
        //需要复制的目标文件或目标文件夹  
        String pathname = "E:\\file\\a\\setting"; 
//        String pathname = "E:\\file\\a\\360安全卫士_9.6正式版.exe"; 
        File file = new File(pathname);  
        //复制到的位置  
        String topathname = "H:\\file\\b";  
        File toFile = new File(topathname);  
        localFileCopy(file, toFile);  
    }
    
    
    public static void localFileCopy(File file, File toFile){
    	long startMili=System.currentTimeMillis();// 当前时间对应的毫秒数
    	try {
        	logger.info("【文件复制】：开始...");
        	fileCopy(file, toFile);  
        } catch (Exception e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
            logger.error("【文件复制】：失败!"+e);
        }finally {
        	logger.info("【文件复制】：结束!耗时:"+(System.currentTimeMillis()-startMili)+"ms!包含 "+fileCount+"个文件,"+dirCount+"个文件夹");
		}
    	
    }
    
    
    /**
     * 文件复制
     * 
     * @param file
     * @param toFile
     * @throws Exception
     */
  
    private static void fileCopy(File file, File toFile) throws Exception {  
        if (file.isDirectory()) {  
        	dirCount++;
        	
            String filepath = file.getAbsolutePath();  
            filepath=filepath.replaceAll("\\\\", "/");  
            String toFilepath = toFile.getAbsolutePath();  
            toFilepath=toFilepath.replaceAll("\\\\", "/");  
            int lastIndexOf = filepath.lastIndexOf("/");  
            toFilepath = toFilepath + filepath.substring(lastIndexOf ,filepath.length());  
            File copy=new File(toFilepath);  
            //复制文件夹  
            if (!copy.exists()) {  
                copy.mkdir(); 
                logger.info("【文件复制】：创建目录"+toFilepath);
            }  
            //遍历文件夹  
            for (File f : file.listFiles()) {  
            	fileCopy(f, copy);  
            }
        } else {  
            if (toFile.isDirectory()) {  
                String filepath = file.getAbsolutePath();  
                filepath=filepath.replaceAll("\\\\", "/");  
                String toFilepath = toFile.getAbsolutePath();  
                toFilepath=toFilepath.replaceAll("\\\\", "/");  
                int lastIndexOf = filepath.lastIndexOf("/");  
                toFilepath = toFilepath + filepath.substring(lastIndexOf ,filepath.length());  
                  
                //写文件  
                File newFile = new File(toFilepath);  
                writeFile(file,newFile);
            } else {  
                //写文件  
                writeFile(file,toFile);
            }  
  
        }  
    }  
    /**
     * 写入文件
     * 
     * @param file
     * @param toFile
     */
    private static void writeFile(File file, File toFile)throws Exception{
    	long startMili=System.currentTimeMillis();// 当前时间对应的毫秒数
    	logger.info("【文件复制】：开始!"+file.getPath()+"--->"+toFile.getPath());
    	byte[] b = new byte[1024];  
    	int a; 
    	FileInputStream fis = null;  
        FileOutputStream fos = null;
    	fis = new FileInputStream(file);  
		fos = new FileOutputStream(toFile);
		while ((a = fis.read(b)) != -1) {  
            fos.write(b, 0, a);  
        }
		fos.close();
		fis.close();
		logger.info("【文件复制】：成功!耗时:"+(System.currentTimeMillis()-startMili)+"ms");
		
		fileCount++;
    }

}
